package org.hybridlabs.source.formatter.m2;

/**
 * Copyright 2008 hybrid labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

/**
 * Run org.hybridlabs.beautifier as a maven plugin after generating sources.
 * Beautifier changes all fully-qualified classname references to import statements.
 *
 * @phase process-sources
 * @goal beautify-imports
 * @requiresProject false
 *
 * @author Jack Archer, Bob Fields
 * @version 1.0, Feb. 2007
 */
public class ImportBeautifierMojo extends AbstractMojo {
	/**
	 * Location of the directory to be recursively beautified. Defaults to all
	 * source directories for parent project (..)
	 *
	 * @parameter expression="${basedir}"
	 * @optional
	 */
	private File inputDirectory;

	/**
	 * Location of the output directory for beautified source. Defaults to the
	 * source directory
	 *
	 * @parameter expression="${basedir}"
	 * @optional
	 */
	private File outputDirectory;

	/**
	 * Run import beautifier utility.
	 *
	 * @parameter default-value="true"
	 * @optional
	 */
	private boolean runBeautifier;

	/**
	 * Delegate formatting to Jalopy after beautifying imports.
	 *
	 * @parameter default-value="false"
	 * @optional
	 */
	private boolean runJalopy;

	/**
	 * Whether or not processing should be skipped (this is if you just want to
	 * force Beautifier not to run on your code, i.e. if generating site from
	 * already formatted source code).
	 *
	 * @parameter expression="${beautifier.run.skip}"
	 */
	private boolean skipProcessing = false;

	public void execute() throws MojoExecutionException {
		if (this.skipProcessing) {
			getLog().info("process-sources:beautify-imports skipProcessing");
			return;
		}
		File file = this.inputDirectory;
		if (!file.exists()) {
			throw new MojoExecutionException(
					"Beautifier format input directory does not exist: "
							+ this.inputDirectory);
		}

		if (this.outputDirectory == null || this.outputDirectory.equals("")) {
			this.outputDirectory = this.inputDirectory;
		} else {
			File outputFile = this.outputDirectory;
			if (!outputFile.exists()) {
				throw new MojoExecutionException(
						"Beautifier format output directory does not exist: "
								+ this.outputDirectory);
			}
		}
		String directoryString = null;

		try {
			directoryString = file.getCanonicalPath();

			FormatManager.formatAllFiles(directoryString, directoryString,
					this.runBeautifier, this.runJalopy);
		} catch (FileNotFoundException e) {
			throw new MojoExecutionException(
					"FileNotFound creating beautifier output: "
							+ directoryString, e);
		} catch (IOException e) {
			throw new MojoExecutionException(
					"Error creating beautifier output: " + directoryString, e);
		}
	}
}
